package com.ff.javamonkey.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.ff.javamonkey.R;
import com.ff.javamonkey.sqlite.type.Acrlac;
import com.ff.javamonkey.sqlite.type.Group;

public class AcrlacListAdapter extends BaseGroupAdapter<Acrlac> {
	// Debugging
	private static final String TAG = "AcrlacListAdapter";
	
	private LayoutInflater mInflater;

	public AcrlacListAdapter(Context ctx) {
		super(ctx);
		mInflater = LayoutInflater.from(ctx);
	}

	@Override
	public void setGroup(Group<Acrlac> g) {
		super.setGroup(g);
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.list_sqlite_item, null);
			viewHolder = new ViewHolder();
			viewHolder.description = (TextView)convertView.findViewById(R.id.txtSqliteItemDescription);
			viewHolder.fileIcon = (ImageView)convertView.findViewById(R.id.imgSqliteItem);
			viewHolder.runningStatus = (TextView)convertView.findViewById(R.id.txtSqliteItemStatus);
			viewHolder.downloadingBar = (ProgressBar)convertView.findViewById(R.id.progressBarDownloading);
			viewHolder.subtitle = (TextView)convertView.findViewById(R.id.txtSqliteItemSubtitle);
			convertView.setTag(viewHolder);
		}
		else {
			viewHolder = (ViewHolder)convertView.getTag();
		}
		Acrlac acrlac = (Acrlac) getItem(position);
		viewHolder.description.setText(acrlac.getmFilename());
		viewHolder.runningStatus.setText(acrlac.getmDataType());
		if (acrlac.isDownloading()) {
			viewHolder.runningStatus.setVisibility(View.GONE);
			viewHolder.downloadingBar.setVisibility(View.VISIBLE);
			viewHolder.subtitle.setVisibility(View.VISIBLE);
			if (acrlac.isCheckingHashFile()) {
				viewHolder.subtitle.setText("Checking hash File");
				viewHolder.downloadingBar.setIndeterminate(true);
			}
			else { 
				viewHolder.subtitle.setText("Downloading " + acrlac.getCurrentFileSize() + " / " + 
					acrlac.getFileSize());
				viewHolder.downloadingBar.setProgress(acrlac.getProgress());
			}
		}
		return convertView;
	}
	
	private static class ViewHolder {
		TextView description;
		TextView runningStatus;
		ImageView fileIcon;
		ProgressBar downloadingBar;
		TextView subtitle;
	}
}
